﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BussinessEntity;
using BussinessLayer;

namespace PresentationLayer
{
    public partial class frmSearchImport : DevComponents.DotNetBar.OfficeForm
    {
        public List<Import> Result { get; set; }
        public List<Import> list = ImportManager.GetAll().Array;

        public frmSearchImport()
        {
            InitializeComponent();
            cbSearchBy.Items.Add("Mã phiếu nhập");
            cbSearchBy.Items.Add("Tên nhà cung cấp");
            cbSearchBy.Items.Add("Ngày nhận");
            cbSearchBy.SelectedIndex = 0;
        }

        private void cbSearchBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbSearchBy.SelectedIndex != 2)
            {
                cbKeywork.Visible = true;
                dtiDate.Visible = false;
                cbKeywork.DataSource = null;
                cbKeywork.DropDownStyle = ComboBoxStyle.DropDown;
                cbKeywork.DataSource = list;
                if (cbSearchBy.SelectedIndex == 0)
                {
                    cbKeywork.DisplayMember = cbKeywork.ValueMember = "ImportID";
                    foreach (var item in list)
                    {

                        cbKeywork.AutoCompleteCustomSource.Add(item.ImportID);
                    }
                }
                else
                    if (cbSearchBy.SelectedIndex == 1)
                    {
                        cbKeywork.DisplayMember = cbKeywork.ValueMember = "SupplierName";
                        foreach (var item in list)
                        {
                            cbKeywork.AutoCompleteCustomSource.Add(item.SupplierName);
                        }
                    }
                cbKeywork.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                cbKeywork.AutoCompleteSource = AutoCompleteSource.CustomSource;

            }
            else
            {
                cbKeywork.Visible = false;
                dtiDate.Visible = true;
                dtiDate.Value = DateTime.Today;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (cbSearchBy.SelectedIndex != 2)
            {
                if (cbSearchBy.SelectedIndex == 0)
                {
                    Result = list.FindAll(s => s.ImportID.Contains(cbKeywork.Text));
                }
                else
                    if (cbSearchBy.SelectedIndex == 1)
                    {
                        Result = list.FindAll(s => s.SupplierName.Contains(cbKeywork.Text));
                    }
            }
            else
                Result = list.FindAll(s => s.ImportDate == dtiDate.Value);
            if (Result.Count > 0)
                Close();
            else
            {
                MessageBox.Show("Không tìm thấy thông tin phù hợp", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}